package com.shawn.integeration.batch;

import com.shawn.integeration.batch.decider.DateDecider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * @author shawn
 * @description: 决策器任务配置
 * @date 2024/1/7
 */
@Slf4j
//@Component
public class DeciderJobBatchConfig {
    @Resource
    private JobBuilderFactory jobFactory;

    @Resource
    private StepBuilderFactory stepFactory;

    @Resource
    private DateDecider dateDecider;

    @Bean
    public Job deciderJob() {
        // 首先执行deciderStep1(),然后使用自定义的日期决策器
        // 如果返回周末weekend那就执行deciderStep2()
        // 如果返回工作日workingDay那就执行deciderStep3()
        // 无论deciderStep3()的结果是什么都会执行deciderStep4()
        return jobFactory.get("deciderJob")
                .start(deciderStep1())
                .next(dateDecider)
                .from(dateDecider).on("weekend").to(deciderStep2())
                .from(dateDecider).on("workingDay").to(deciderStep3())
                .from(deciderStep3()).on("*").to(deciderStep4())
                .end()
                .build();
    }

    private Step deciderStep1() {
        return stepFactory.get("deciderStep1")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("deciderStep1执行步骤一。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step deciderStep2() {
        return stepFactory.get("deciderStep2")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("deciderStep2执行步骤二。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step deciderStep3() {
        return stepFactory.get("deciderStep3")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("deciderStep3执行步骤三。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }

    private Step deciderStep4() {
        return stepFactory.get("deciderStep4")
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("deciderStep4执行步骤四。。。");
                    return RepeatStatus.FINISHED;
                }).build();
    }
}
